package com.ego.controller;

import com.ego.dao.DressDao;
import com.ego.dao.impl.DressDaoImpl;
import com.ego.pojo.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author 袁梦达 2019012364
 */
@WebServlet("/DressServlet")
public class DressServlet extends HttpServlet {

    private DressDao dressDao = new DressDaoImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取详细展示的商品的ID
        InputStream is = request.getInputStream();

        StringBuffer stringBuffer = new StringBuffer();
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = is.read(bytes)) != -1){
            stringBuffer.append(new String(bytes, 0, len));
        }
        int id = Integer.parseInt(stringBuffer.toString());
        Product product = dressDao.getProduct(id);
        String msg = product.getId() + "#"
                + product.getImage() + "#"
                + product.getBaseInformation() + "#"
                + product.getPrice() + "#"
                + product.getDetail() + "#"
                + product.getBrand() + "#"
                + product.getSize() + "#"
                + product.getDiscount();
        response.getWriter().print(msg);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
